import { fetchGoodsList } from '../../services/good/fetchGoods';

Page({
  data: {
    imgSrcs: [],
    tabList: [],
    goodsList: [],
    goodsListLoadStatus: 0,
    pageLoading: false,
    current: 1,
    autoplay: true,
    duration: '500',
    interval: 5000,
    navigation: { type: 'dots' },
    swiperImageProps: { mode: 'scaleToFill' },
  },

  goodListPagination: {
    index: 0,
    num: 20,
  },

  privateData: {
    tabIndex: 0,
  },

  onShareAppMessage: function () {
    return {
      title: '枫味日记',
      path: '/pages/home/home',
      imageUrl: 'https://gitee.com/chen-feng1111/mycookbook/raw/master/images/category/share.jpg'
    };
  },
  onShareTimeline: function () {
    return {
      title: '枫味日记',
      imageUrl: 'https://gitee.com/chen-feng1111/mycookbook/raw/master/images/category/share.jpg'
    };
  },

  onShow() {
    this.getTabBar().init();
  },

  onLoad() {
    this.init();
  },

  // onReachBottom() {
  //   if (this.data.goodsListLoadStatus === 0) {
  //     this.loadGoodsList();
  //   }
  // },

  // onPullDownRefresh() {
  //   this.init();
  // },

  init() {
    this.loadHomePage();
  },

  loadHomePage() {

    this.loadGoodsList(true);

  },

  // tabChangeHandle(e) {
  //   this.privateData.tabIndex = e.detail;
  //   this.loadGoodsList(true);
  // },

  // onReTry() {
  //   this.loadGoodsList();
  // },

  async loadGoodsList(fresh = false) {
    try {
      const nextList = await fetchGoodsList(0, 0);
      this.setData({
        goodsList: nextList,
        goodsListLoadStatus: 0,
      });

      this.goodListPagination.index = 0;
      this.goodListPagination.num = 100;
    } catch (err) {
      console.log("error", err)
      this.setData({ goodsListLoadStatus: 3 });
    }
  },

  goodListClickHandle(e) {
    const { index } = e.detail;
    const { saasId } = this.data.goodsList[index];

    wx.navigateTo({
      url: `/pages/goods/details/index?saasId=${saasId}`,
    });
  },

});
